<?php
/**
 * Created by PhpStorm.
 * User: Jason
 * Date: 2017/9/18
 * Time: 17:19
 */

namespace Home\Controller;


use Admin\Enums\AuthorType;
use Home\Service\ImageScoreService;
use Home\Service\ImageSheetService;
use Home\Service\ScoreCommentService;
use Home\Service\ScoreFavoriteService;
use Home\Service\ScoreService;
use Home\Service\SheetService;
use Think\Image;
use Think\Page;

class ScoreController extends HomeController
{
    /**
     * 获取谱单详情
     */
    public function index()
    {
        $id = I('id');
        $user = session('user');
        $uid = $user['uid'];
        if(!empty($id) && is_numeric($id)) {

            //1.获取谱单信息及创建者信息
            $map = array(
                'id' => $id,
                'is_delete' => 0
            );
            $scoreInfo = M('Score')->where($map)->select();
            $service = new ScoreService();
            $scoreInfo = $service->scoreInfo($scoreInfo);
            //获取登录用户是否收藏谱单
            $scoreFavService = new ScoreFavoriteService();
            $favStatus = $scoreFavService->haveFav($id, $uid);
            $scoreInfo[0]['fav_status'] = $favStatus ? 1 : 0;
            $this->assign('scoreInfo', $scoreInfo[0]);

            //2.获取谱单内曲谱
            $map = array(
                'score_id' => $id,
                'ss.is_delete' => 0
            );
            $count = M('ScoreSheet')->alias('ss')->join('sheet sh ON ss.sheet_id = sh.id')->where($map)->count();

            $page = new Page($count, 10);
            $page->setConfig('prev', '');
            $page->setConfig('next', '');
            $page->setConfig('theme', "%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
            $show = $page->show();
            $sheetList = M('ScoreSheet')->alias('ss')->join('sheet sh ON ss.sheet_id = sh.id')->where($map)
                ->order('ss.id')->limit($page->firstRow.','.$page->listRows)->select();
            if (!empty($sheetList)) {
                $sheetService = new SheetService();
                $sheetList = $sheetService->getSheetInfo($sheetList);
            }
            $this->assign('page', $show);
            $this->assign('sheetList', $sheetList);

            //3.获取谱单评论
            $commentService = new ScoreCommentService();
            $comment = $commentService->comment($id);
            $this->assign('comments', $comment);
        }
        $this->display();
    }

    /*
     * 获取用户创建的谱单
     */
    public function collectSheet()
    {
        $data = I('param.');
        $user = session('user');
        $data['user_id'] = $user['uid'];
        $service = new ScoreService();
        $list = $service->getScoreByUesrId($user['uid']);

        $this->assign('sheet_id', $data['id']);
        $this->assign('scores',$list);
        $this->display('score_list');
    }

    /**
     * 新建谱单并收藏的页面(收藏曲谱功能)
     */
    public function addScore()
    {
        $data = I('param.');
        $this->assign('sheet_id', $data['id']);
        $this->display('add_score');
    }

    /**
     * 谱单设置
     */
    public function config($id)
    {
        if (IS_POST) {

        } else {
            //1.获取谱单信息
            $list = M('Score')->where(array('id'=>$id))->field('id, name, desc')->find();
            if (empty($list)) {
                $this->fail(500, '', array('获取谱单信息失败'));
            }
            //2.获取图片
            $imgService = new ImageScoreService();
            $img = $imgService->ScoreImg($id);
            $list['img'] = $img[$id]['url'];
            $this->assign('score', $list);
            $this->display('config');
        }
    }

    /**
     * 获取我喜欢的曲谱内容
     */
    public function myFavSheet()
    {
        $uid = I('uid');
        //获取个人信息
        $user = M('User')->where(['id' => $uid])->field('id, nickname, avatar_url')->find();


        //2.获取谱单内曲谱
        $map = [
            'user_id' => $uid,
            'sf.is_delete' => 0
        ];
        $count = M('SheetFavorite')->alias('sf')->join('sheet sh ON sf.sheet_id = sh.id')->where($map)->count();

        $page = new Page($count, 10);
        $page->setConfig('prev', '');
        $page->setConfig('next', '');
        $page->setConfig('theme', "%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $show = $page->show();
        $sheetList = M('SheetFavorite')->alias('sf')->join('sheet sh ON sf.sheet_id = sh.id')->where($map)
            ->order('sf.id')->limit($page->firstRow.','.$page->listRows)->select();
        if (!empty($sheetList)) {
            $sheetService = new SheetService();
            $sheetList = $sheetService->getSheetInfo($sheetList);
        }

        $this->assign('user', $user);
        $this->assign('page', $show);
        $this->assign('sheetList', $sheetList);
        $this->display('my_fav_sheet');
    }

    /**
     * 获取更多谱单评论
     */
    public function getComment($scoreId, $page, $limit)
    {
        if (!$scoreId || !is_numeric($scoreId)) {
            $result['code'] = 600;
            $result['msg']  = 'error scoreId';
            $result['data'] = [];
            $this->ajaxReturn($result);
        }
        $commentService = new ScoreCommentService();
        $comment = $commentService->comment($scoreId, $page, $limit);
        if (!$comment['list']) {
            $result['code'] = 600;
            $result['msg']  = 'no data';
            $result['data'] = [];
            $this->ajaxReturn($result);
        }
        $result['code'] = 200;
        $result['msg']  = 'success';
        $result['data'] = $comment['list'];
        $this->ajaxReturn($result);
    }

    /**
     * 添加评论
     */
    public function comment()
    {
        $param = I('param.');
        $user = session('user');

        if (!$user || $user['uid'] != $param['user_id']) {
            $result['code'] = 101;
            $result['msg']  = '登录失效';
            $result['data'] = [];
            $this->ajaxReturn($result);
        }
        if (!$param['score_id']) {
            $result['code'] = 600;
            $result['msg']  = '评论失败!';
            $result['data'] = [];
            $this->ajaxReturn($result);
        }
        $model = new ScoreCommentService();
        $data = $model->addComment($param);
        if (!$data) {
            $result['code'] = 600;
            $result['msg']  = '评论失败!';
            $result['data'] = [];
            $this->ajaxReturn($result);
        }
        $result['code'] = 200;
        $result['msg']  = '评论成功!';
        $result['data'] = $data;
        $this->ajaxReturn($result);
    }

    /**
     * 收藏谱单
     */
    public function favScore()
    {
        $data = I('param.');
        $user = session('user');
        if (empty($user)) {
            $return['code'] = 10001;
            $return['msg'] = '请先登录';
            $this->ajaxReturn($return);
        }
        $data['user_id'] = $user['uid'];
        $favService = new ScoreFavoriteService();
        $res = $favService->favScore($data);
        if ($res) {
            $return['code'] = 200;
            $return['msg'] = 'success';
            $this->ajaxReturn($return);
        } else {
            $return['code'] = 0;
            $return['msg'] = 'failed';
            $this->ajaxReturn($return);
        }
    }
}